package com.jiangyg.mall.product.controller;

import com.jiangyg.mall.core.support.restful.Pagination;
import com.jiangyg.mall.core.support.restful.Result;
import com.jiangyg.mall.product.dto.SpuAttrDTO;
import com.jiangyg.mall.product.dto.SpuSaveDTO;
import com.jiangyg.mall.product.vo.SpuInfoVO;
import com.jiangyg.mall.product.service.AttrgroupService;
import com.jiangyg.mall.product.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 类描述：spu信息控制器
 *
 * @author jiangyg
 * @version email@jiangyg.com
 * @date 2021-10-30 07:38:48
 */
@RestController
@RequestMapping("/product/spu")
public class SpuController {

    /**
     * spu信息接口
     */
    private final SpuService spuService;

    /**
     * 属性分组接口
     */
    private final AttrgroupService attrgroupService;

    @Autowired
    public SpuController(SpuService spuService, AttrgroupService attrgroupService) {
        this.spuService = spuService;
        this.attrgroupService = attrgroupService;
    }

    /**
     * 功能描述：商品属性分页查询
     *
     * @param pagination 分页参数
     * @return 返回分页商品属性
     */
    @ResponseBody
    @PostMapping("/select")
    public Result<Pagination<SpuInfoVO>> select(@RequestBody Pagination<SpuInfoVO> pagination) throws Exception {
        return Result.ok(spuService.selectPagination(pagination));
    }

    /**
     * 功能描述：保存spu信息
     *
     * @param request 请求
     * @param dto     spu信息
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/save")
    public Result<Object> save(HttpServletRequest request, @RequestBody @Validated SpuSaveDTO dto) throws Exception {
        return Result.saveOrUpdate(spuService.save(dto));
    }

    /**
     * 功能描述：SPU上架
     *
     * @param request 请求
     * @param spuId   spuId
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/published")
    public Result<Object> published(HttpServletRequest request, @RequestParam("spuId") Long spuId) throws Exception {
        return Result.saveOrUpdate(spuService.published(spuId));
    }

    /**
     * 功能描述：SPU属性分组信息
     *
     * @param spuId      spuId
     * @param categoryId 分组ID
     * @return 返回分页属性分组
     */
    @ResponseBody
    @PostMapping("/attr-attrgroup-list")
    public Result<Object> listAttachAttr(@RequestParam("spuId") Long spuId, @RequestParam("categoryId") Long categoryId) throws Exception {
        final Map<String, Object> data = new HashMap<>();
        // 属性分组信息
        data.put("attrgroups", attrgroupService.listAttachAttr(categoryId));
        // SPU 属性信息
        data.put("baseAttrs", spuService.listAttr(spuId));
        return Result.ok(data);
    }

    /**
     * 功能描述：保存spu信息
     *
     * @param spuId     spuId
     * @param baseAttrs 规则信息
     * @return 结果
     */
    @ResponseBody
    @PostMapping("/saveBaseAttrs")
    public Result saveBaseAttrs(@RequestParam("spuId") Long spuId, @RequestBody @Validated List<SpuAttrDTO> baseAttrs) {
        return Result.saveOrUpdate(spuService.saveBaseAttrs(spuId, baseAttrs));
    }

}
